package com.blb.community.controller.upload;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.blb.community.Utils.CodeUtiles;
import com.blb.community.Utils.OssUtil;
import com.blb.community.pojo.ResponseResult;
import com.blb.community.pojo.VO.YZM;
import com.blb.community.pojo.ZyOwner;
import com.blb.community.service.ZyOwnerService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;


@RestController
public class UpdateController {

    @Resource
    private ZyOwnerService zyOwnerService;

    @PostMapping("/upload")
    public String upload(MultipartFile file, @RequestParam String username) throws IOException {
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        OssUtil.upload(file.getInputStream(),s+".jpg");
        String url = OssUtil.getURL(s + ".jpg");
        int i = url.indexOf("?");
        String substring = url.substring(0, i);
        zyOwnerService.update(new UpdateWrapper<ZyOwner>().set("owner_signature",substring).eq("owner_phone_number",username));
        return substring;
    }


    @PostMapping("/upload/repair")
    public String uploadRepair(MultipartFile file) throws IOException {
        StringBuffer stringBuffer=new StringBuffer();
            UUID uuid = UUID.randomUUID();
            String s = uuid.toString();
            OssUtil.upload(file.getInputStream(),s+".jpg");
            String url = OssUtil.getURL(s + ".jpg");
            int i = url.indexOf("?");
            String substring = url.substring(0, i);
            stringBuffer.append(substring);
        return stringBuffer.toString();
    }

    //获取验证码
    @PostMapping("/yzm")
    public void uploadYZM(HttpServletResponse response){
        Map<String, Object> map = null;
        try {
            File file = new File("c:/yzm");
            if(!file.exists()){
                file.mkdirs();
            }
            map = CodeUtiles.getCodeImage();
            ImageIO.write((RenderedImage) map.get("img"), "jpg", new FileOutputStream("c:/yzm/yzm.jpg"));
            OssUtil.upload(new FileInputStream("c:/yzm/yzm.jpg"),"yzm.jpg");
//            response.setHeader("Access-Control-Allow-Origin","http://localhost:9528");
//            response.setHeader("Access-Control-Allow-Methods","*");
//            response.setHeader("Access-Control-Allow-Credentials","true");
            ResponseResult.write(ResponseResult.ok(new YZM(OssUtil.getURL("yzm.jpg"),(String) map.get("code"))),response);
        } catch (IOException e) {

        }
    }

    @PostMapping("/complaintSuggest/uploadImage")
    public ResponseResult uploadImage(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        OssUtil.upload(file.getInputStream(),filename);
        String url = OssUtil.getURL(filename);
        String substring = url.substring(0, url.indexOf("?"));
        return ResponseResult.ok(substring);
    }

}